// pages/movies/movies.js
/* 命名导出,也可以分开写两次，导入的时候带花括号 */
import base_href from '../../asset/js/g-mixins';
import util from "../../asset/js/util";

var app = getApp();
Page({
    data: {
        /* 初始值 getMovieListData是异步的，页面刚加载的时候会获取不到这三个初始值 */
        inTheaters: {},
        comingSoon: {},
        top250: {},
        containerShow: true,
        searchPanelShow: false,
        searchResult:{},
        serachValue:'',
    },
    onLoad: function (event) {
        var inTheatersUrl = app.globalData.doubanBase + '/v2/movie/in_theaters';
        var comingSoonUrl = app.globalData.doubanBase + '/v2/movie/coming_soon';
        var top250Url = app.globalData.doubanBase + '/v2/movie/top250';
        this.getMovieListData(inTheatersUrl, 3, "inTheaters", "正在热映");
        this.getMovieListData(comingSoonUrl, 3, "comingSoon", "即将上映");
        this.getMovieListData(top250Url, 3, "top250", "豆瓣Top250");
    },
    /* 发送get请求，获取电影信息 */
    getMovieListData: function (url, count, movieListKey, movieListTitle) {
        var $this = this;
        wx.request({
            url: url,
            method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
            header: {
                "Content-Type": "json"
            }, // 设置请求的 header
            data: {
                start: 0,
                count: count
            },
            success: function (res) {
                // success
                console.log(res);
                $this.processDoubanData(res.data, movieListKey, movieListTitle);
            },
            fail: function (error) {
                // fail
                console.log("调用失败");
                console.log(error);
            },
            complete: function () {
                // complete
            }
        })
    },
    /* 处理设置电影分类信息 */
    processDoubanData: function (moviesDouban, movieListKey, movieListTitle) {
        var movies = [];
        for (var index in moviesDouban.subjects) {
            var subject = moviesDouban.subjects[index];
            var title = subject.title;
            var temp = {
                stars: util.convertToStarsArray(subject.rating.stars),
                title: title,
                average: subject.rating.average,
                coverageUrl: subject.images.large,
                movieId: subject.id
            }
            movies.push(temp);
        }
        var readyData = {};
        readyData[movieListKey] = {
            movieListTitle: movieListTitle,
            movies: movies
        }
        this.setData(readyData);
    },
    /* 跳转到更多页面 */
    goToMoreMovie: function (event) {
        var movieListTitle = event.currentTarget.dataset.movielisttitle;
        wx.navigateTo({
            url: "more-movie/more-movie?movieListTitle=" + movieListTitle
        })
        // console.log(event);
    },
    /* 跳转到电影详情页面 */
    goToMovieDetail: function (event) {
        var movieId = event.currentTarget.dataset.movieid;
        wx.navigateTo({
            url: "movie-detail/movie-detail?id="+movieId
        })
        // console.log(event);
    },
    /* 搜索层 显示 */
    onBindFocus: function (event) {
        this.setData({
            containerShow: false,
            searchPanelShow: true
        });
    },
    /* 搜索层 隐藏 */
    onCanceTap: function (event) {
        this.setData({
            containerShow: true,
            searchPanelShow: false,
            searchResult:{},
            serachValue:''
        });
    },
    /* 搜索框状态改变 */
    onBindChange: function (event) {
        var text = event.detail.value;
        var searchUrl = app.globalData.doubanBase + "/v2/movie/search?q=" + text;
        this.getMovieListData(searchUrl, 20, "searchResult", "");
    },
});